﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Crm.CrmClient;

namespace Crm.Views
{
    public partial class Kampanya : ChildWindow
    {
        public bool EskiEvrak { get; set; }
        public int KampanyaID { get; set; }
        KampanyaTanimlari kmp = new KampanyaTanimlari();
        public Kampanya()
        {
            InitializeComponent();
            this.Loaded += Kamp_Loaded;
            srv.KampanyaKaydetCompleted += srv_KampanyaKaydetCompleted;
            srv.KampanyaGetirCompleted += srv_KampanyaGetirCompleted;
            srv.KampanyaSilCompleted += srv_KampanyaSilCompleted;
            btnKod.Click += btnKod_Click;
            btnUnvan.Click += btnUnvan_Click;
            srv.MaxKampanyaNoCompleted += srv_MaxKampanyaNoCompleted;
            srv.KampanyaTanimSilCompleted += srv_KampanyaTanimSilCompleted;
        }

        void srv_KampanyaTanimSilCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {

        }

        void srv_MaxKampanyaNoCompleted(object sender, MaxKampanyaNoCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                txtNo.Text = e.Result.ToString();
            }
        }

        void btnUnvan_Click(object sender, RoutedEventArgs e)
        {
            PopupList list = new PopupList();
            list.ListeTipi = PopupListeTipleri.Cari;
            list.KodunaGore = false;
            list.Filtre = txtFirmaUnvan.Text.Replace('*', '%');
            list.Closed += list_Closed;
            list.Show();
        }

        void btnKod_Click(object sender, RoutedEventArgs e)
        {
            PopupList list = new PopupList();
            list.ListeTipi = PopupListeTipleri.Cari;
            list.KodunaGore = true;
            list.Filtre = txtFirmaKodu.Text.Replace('*', '%');
            list.Closed += list_Closed;
            list.Show();
        }

        void list_Closed(object sender, EventArgs e)
        {
            PopupList ls = (PopupList)sender;
            if (ls.DialogResult == true)
            {
                if (ls.ListeTipi == PopupListeTipleri.Cari || ls.ListeTipi == PopupListeTipleri.Aday)
                {
                    txtFirmaKodu.Text = ls.Result["Kodu"].ToString();
                    txtFirmaUnvan.Text = ls.Result["Adi"].ToString();
                    txtFirmaKodu.Tag = ls.Result["ID"];
                }
                else if (ls.ListeTipi == PopupListeTipleri.Stok)
                {
                    KampanyaList item = (KampanyaList)dataGrid.SelectedItem;
                    if (item != null)
                    {
                        item.StokKod = ls.Result["StokKod"].ToString();
                        item.Adi = ls.Result["Adi"].ToString();
                        item.Fiyat = Convert.ToDouble(ls.Result["Fiyat"]);
                    }
                }
            }
        }

        int counter = 0;
        void srv_KampanyaKaydetCompleted(object sender, KampanyaKaydetCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                counter--;
            }

            if (counter == 0)
            {
                MessageBox.Show("Kaydedildi !");
                DialogResult = true;
            }
        }

        void srv_KampanyaSilCompleted(object sender, KampanyaSilCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                MessageBox.Show("Satır Silindi !");
            }
        }

        void srv_KampanyaGetirCompleted(object sender, KampanyaGetirCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                kmp = e.Result as KampanyaTanimlari;
                txtAdi.Text = kmp.KampanyaAdi;
                cmbTipi.SelectedIndex = kmp.KampanyaTipi;
                txtFirmaKodu.Text = kmp.FirmaKodu;
                txtFirmaUnvan.Text = kmp.FirmaUnvan;
                txtNo.Text = kmp.No.ToString();
                dataGrid.ItemsSource = kmp.GridList;
            }
        }

        CrmClient.CrmServiceClient srv = new CrmClient.CrmServiceClient();
        void Kamp_Loaded(object sender, RoutedEventArgs e)
        {
            if (!EskiEvrak)
            {
                txtFirmaKodu.Visibility = System.Windows.Visibility.Collapsed;
                txtFirmaUnvan.Visibility = System.Windows.Visibility.Collapsed;
                btnKod.Visibility = System.Windows.Visibility.Collapsed;
                btnUnvan.Visibility = System.Windows.Visibility.Collapsed;
                kmp.GridList = new ObservableCollection<KampanyaList>();
                dataGrid.ItemsSource = kmp.GridList;
                srv.MaxKampanyaNoAsync();
            }
            else
            {
                srv.KampanyaGetirAsync(KampanyaID);
            }

        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            counter = kmp.GridList.Count;
            if (kmp.ID != 0)
                srv.KampanyaTanimSilAsync(kmp.No);
            foreach (var item in kmp.GridList)
            {
                srv.KampanyaKaydetAsync(-1, int.Parse(txtNo.Text), txtAdi.Text, Convert.ToByte(cmbTipi.SelectedIndex), txtFirmaKodu.Text, txtFirmaUnvan.Text, item.StokKod, item.Adi, item.Fiyat);
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (kmp.ID != 0)
                srv.KampanyaTanimSilAsync(kmp.No);

            kmp.GridList = new ObservableCollection<KampanyaList>();
            dataGrid.ItemsSource = kmp.GridList;
            foreach (var item in this.grid.Children)
            {
                if (item.GetType() == typeof(TextBox))
                {
                    ((TextBox)item).Text = "";
                }
                if (item.GetType() == typeof(ComboBox))
                {
                    ((ComboBox)item).SelectedIndex = 0;
                }
            }
        }

        private void btnSil_Click_1(object sender, RoutedEventArgs e)
        {
            var ctl = e.OriginalSource as Button;
            if (null != ctl)
            {
                var sip = ctl.DataContext as KampanyaList;
                if (null != sip)
                {
                    if (sip.ID != 0)
                    {
                        srv.KampanyaSilAsync(sip.ID);
                    }
                    kmp.GridList.Remove(sip);
                }
            }
        }

        private void btnYeniSatir_Click_1(object sender, RoutedEventArgs e)
        {
            kmp.GridList.Add(new KampanyaList { Adi = "" });
        }

        private void cmbTipi_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbTipi.SelectedItem.ToString() != "Genel")
            {
                txtFirmaKodu.Visibility = System.Windows.Visibility.Visible;
                txtFirmaUnvan.Visibility = System.Windows.Visibility.Visible;
                btnKod.Visibility = System.Windows.Visibility.Visible;
                btnUnvan.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                txtFirmaKodu.Visibility = System.Windows.Visibility.Collapsed;
                txtFirmaUnvan.Visibility = System.Windows.Visibility.Collapsed;
                btnKod.Visibility = System.Windows.Visibility.Collapsed;
                btnUnvan.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void btnBul_Click_1(object sender, RoutedEventArgs e)
        {
            if (dataGrid.SelectedIndex > -1)
            {
                KampanyaList item = (KampanyaList)dataGrid.SelectedItem;
                PopupList list = new PopupList();
                list.ListeTipi = PopupListeTipleri.Stok;
                list.KodunaGore = true;
                list.Filtre = item.Adi.Replace('*', '%');
                list.Closed += list_Closed;
                list.Show();
            }
        }

    }
}

